package week11;

import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

public class FindInArrTask extends RecursiveTask<Integer>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static int[] arr;
	private int begin,end;
	
	
	public FindInArrTask(int[] arr,int begin,int end) {
		FindInArrTask.arr = arr;
		this.begin = begin;
		this.end = end;
	}

	@Override
	protected Integer compute() {
		if(end-begin<10000){
			return ArrayUtil.findMax(arr, begin, end);
		}
		int mid = (begin+end)/2;
		ForkJoinTask<Integer> leftFork = new FindInArrTask(arr, begin, mid).fork();
		ForkJoinTask<Integer> rightFork = new FindInArrTask(arr, mid+1, end).fork();
		
		return leftFork.join()>rightFork.join()?leftFork.join():rightFork.join();
	}
	
}

